package com.wetao.module_demo_cyh;

import android.content.Intent;
import android.view.View;
import android.widget.Button;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.orhanobut.logger.Logger;
import com.wetao.lib_common.ConstantArouter;
import com.wetao.lib_common.Constants;
import com.wetao.lib_common.base.BaseActivity;
import com.wetao.lib_common.base.BaseResponse;
import com.wetao.lib_common.tsdBlePen.TsdService;
import com.wetao.lib_common.utils.ComUtil;
import com.wetao.lib_common.utils.JsonUtils;
import com.wetao.lib_common.utils.LogUtils;
import com.wetao.lib_common.utils.SPUtils;
import com.wetao.lib_common.utils.oss.OssParams;
import com.wetao.lib_common.utils.oss.UploadOss;
import com.wetao.module_demo_cyh.bean.req.Loginbean;
import com.wetao.module_demo_cyh.bean.resp.MarkingListResp;
import com.wetao.module_demo_cyh.contract.CyhMainContract;
import com.wetao.module_demo_cyh.presenter.CyhMainPresenter;

import java.text.SimpleDateFormat;
import java.util.Date;

import butterknife.BindView;
import butterknife.OnClick;

@Route(path = ConstantArouter.PATH_DEMOCYH_CYHMAINACTIVITY)
public class CyhMainActivity extends BaseActivity<CyhMainContract.View, CyhMainPresenter>
        implements CyhMainContract.View {
    private static final String TAG = "CyhMainActivity";
    @BindView(R2.id.btn_LeakCanaryErr)
    Button btn_LeakCanary;
    @Override
    public int getLayoutId() {
        return R.layout.demo_cyh_main_activity;
    }

    @Override
    public CyhMainPresenter createPresenter() {
        return new CyhMainPresenter(this);
    }

    @Override
    public CyhMainContract.View createView() {
        return this;
    }

    @Override
    public void getLoginResult(BaseResponse response) {
        LogUtils.i("--" + response.getData().toString());
        if(JsonUtils.isGoodJson(response.getData().toString())) {
            Loginbean loginbean = JsonUtils.fromJson(response.getData().toString(), Loginbean.class);
            updateUserConfig(loginbean);
            LogUtils.i(loginbean.toString());
        }
    }

    @Override
    public void getLoginFailed() {

    }

    @Override
    public void getMarkingListResult(BaseResponse response) {

    }

    @Override
    public void getMarkingListFailed() {

    }

    @Override
    public boolean getIsBindTsdService() {
        return false;
    }

    @Override
    public TsdService.OnDataReceiveListener getTsdServiceDataReceiveListener() {
        return null;
    }

    @Override
    public void init() {
    }

    /**
     * 更新用户信息
     * @param loginbean
     */
    private void updateUserConfig(Loginbean loginbean) {
        if(null == loginbean){
            return;
        }

        SPUtils.getInstance().put(Constants.HEAD_XTOKEN, loginbean.getToken());
        //SPUtils.getInstance().put(Constants.USER_ID_KEY, loginbean.getMobile());
    }

    @OnClick({R2.id.btn_LeakCanaryErr,
            R2.id.btn_voiceRecorder,
            R2.id.btn_graffiti,
            R2.id.btn_doodle,
            R2.id.btn_bugly,
            R2.id.btn_markingList,
            R2.id.btn_uploadOSS,
            R2.id.btn_smart,
            R2.id.btn_webview,
            R2.id.btn_login,
            R2.id.btn_OidActivity
    })
    public void click(View view) {
        if(view.getId() == R.id.btn_LeakCanaryErr) {
            Logger.i(TAG, "-----");
            LeakCanaryErr.getInstance(this).dealData();
        } else if(view.getId() == R.id.btn_voiceRecorder) {
            startActivity(new Intent(this, VoiceRecorderActivity.class));
        } else if(view.getId() == R.id.btn_graffiti) {
            
        } else if(view.getId() == R.id.btn_doodle) {

        } else if(view.getId() == R.id.btn_bugly) {
            //CrashReport.testJavaCrash();
        } else if(view.getId() == R.id.btn_login) {
            /*getPresenter().getLogin(ComUtil.getHashmapByStr(
                    new String[]{"mobile", "password"},
                    new String[]{"18779872891", "123456"})
                    , false, false);*/
            getPresenter().getLogin(ComUtil.getHashmapByStr(
                    new String[]{"mobile", "password"},
                    new String[]{"18955909913", "123456"})
                    , false, false);
        } else if(view.getId() == R.id.btn_markingList) {
            String str = "{\"source\":\"\",\"keyword\":\"\",\"state\":2,\"pageNo\":1}";
            MarkingListResp markingListResp = JsonUtils.fromJson(str, MarkingListResp.class);
            getPresenter().getMarkingList(markingListResp
                    , false, false);
        } else if(view.getId() == R.id.btn_uploadOSS) {
            String filePath = "/mnt/sdcard/1532566105209.png";
            final String uploadFileName = new SimpleDateFormat("yyyy-MM-dd").format(new Date()) + "/big.png";
            UploadOss.getInstance().upload2OSS(uploadFileName, filePath, ossUploadListener, null);
        } else if(view.getId() == R.id.btn_smart) {
            startActivity(new Intent(this, SmartActivity.class));
        } else if(view.getId() == R.id.btn_webview) {
            ARouter.getInstance().build(ConstantArouter.PATH_WEBVIEW_WEBMAINACTIVITY).navigation();
        } else if(view.getId() == R.id.btn_OidActivity){

        }
    }

    UploadOss.OnUploadListener ossUploadListener = new UploadOss.OnUploadListener() {
        @Override
        public String uploadSuccess(String uploadFile, String localFile, OssParams ossParams) {
            LogUtils.i("上传文件成功--回调");
            return null;
        }

        @Override
        public String uploadFail(String uploadFile, String localFile, OssParams ossParams) {
            LogUtils.i("上传文件失败--回调");
            return null;
        }
    };

}
